import { calculateBollSeries } from './BOLLMA'

const breakoutColors = {
  upperFill: 'rgba(0, 150, 136, 0.15)',
  lowerFill: 'rgba(255, 152, 0, 0.15)',
  midBull: '#089981',
  midBear: '#f23645'
}

const indicator = {
  name: 'BOLL_BREAKOUT_BANDS',
  shortName: 'BOLL55',
  zLevel: -1,
  calcParams: [55, 1],
  figures: [
    {
      key: 'upperShade',
      type: 'polygon',
      attrs: ({ coordinate }) => {
        const { current, next } = coordinate
        if (
          !current ||
          !next ||
          current.mid == null ||
          current.up == null ||
          next.mid == null ||
          next.up == null
        ) {
          return null
        }
        return {
          coordinates: [
            { x: current.x, y: current.mid },
            { x: next.x, y: next.mid },
            { x: next.x, y: next.up },
            { x: current.x, y: current.up }
          ]
        }
      },
      styles: () => ({
        style: 'fill',
        color: breakoutColors.upperFill
      })
    },
    {
      key: 'lowerShade',
      type: 'polygon',
      attrs: ({ coordinate }) => {
        const { current, next } = coordinate
        if (
          !current ||
          !next ||
          current.mid == null ||
          current.dn == null ||
          next.mid == null ||
          next.dn == null
        ) {
          return null
        }
        return {
          coordinates: [
            { x: current.x, y: current.mid },
            { x: next.x, y: next.mid },
            { x: next.x, y: next.dn },
            { x: current.x, y: current.dn }
          ]
        }
      },
      styles: () => ({
        style: 'fill',
        color: breakoutColors.lowerFill
      })
    },
    {
      key: 'up',
      title: 'UP55: ',
      type: 'line',
      styles: () => ({
        color: '#FF6E6E',
        size: 1.6
      })
    },
    {
      key: 'mid',
      title: 'MID55: ',
      type: 'line',
      styles: (data, indicator, defaultStyles) => {
        const trend = data?.current?.indicatorData?.trend ?? 0
        if (trend > 0) {
          return { color: breakoutColors.midBull }
        }
        if (trend < 0) {
          return { color: breakoutColors.midBear }
        }
        return { color: defaultStyles.lines?.[0]?.color ?? '#FFFFFF' }
      }
    },
    {
      key: 'dn',
      title: 'DN55: ',
      type: 'line',
      styles: () => ({
        color: '#26A69A',
        size: 1.6
      })
    }
  ],
  calc: (dataList, indicatorInstance) => {
    const base = calculateBollSeries(dataList, indicatorInstance.calcParams)
    return base.map((item, index) => {
      const close = dataList[index]?.close
      if (item && item.mid != null && item.up != null && item.dn != null && close != null) {
        const trend = close > item.up ? 1 : close < item.dn ? -1 : 0
        return {
          ...item,
          trend,
          upperShade: item.mid,
          lowerShade: item.mid
        }
      }
      return {
        ...item,
        trend: 0,
        upperShade: null,
        lowerShade: null
      }
    })
  }
}

export default indicator
